app.controller('md5Ctrl', function ($scope, $http, $location) {
  pageInit(-1);
  $("#nav-bar").scope().isFix = true;

  $scope.apps = {
    'nk': '内控4.0',
    'fsk': "全面风险",
    'crk': "信用评级"
  };

  $http.get('/md5/init').then(function (response) {
    if (response.status == 200) {
      var data = response.data;
      if (data.code == 0) {
        $scope.allApp = data.data;
        $scope.nowAppKey = 'nk';
        $scope.nowApp = $scope.allApp['nk'];
      } else {
        $.showNotice(data.msg, "danger");
      }
    }
  }).catch(function (response) {
    $.showNotice(response.status + " error：" + response.data.message, "danger");
  });

  $scope.radio = function (key) {
    $scope.nowAppKey = key;
    $scope.nowApp = $scope.allApp[key];
  }

  $scope.pageItemNum = 15;
  $scope.pageItemNumArr = [];
  for (var i = 0; i < $scope.pageItemNum; i++)
    $scope.pageItemNumArr.push(i);

  function getCompileItems(users) {
    $scope.loading = true;
    $http.post('/md5/execute', {
      users: users
    }).then(function (response) {
      $scope.loading = false;
      if (response.status == 200) {
        var message = response.data;
        if (message.code == 0) {
          $('#md5-dialog').modal('show');

          $scope.compileItems = message.data;
          $scope.compileItemsTotal = message.data.length;

          var pageNum = $scope.compileItemsTotal % $scope.pageItemNum == 0 ? $scope.compileItemsTotal / $scope.pageItemNum - 1 : $scope.compileItemsTotal / $scope.pageItemNum;

          $scope.pages = [];
          for (var i = 0; i < pageNum; i++)
            $scope.pages.push(i + 1);
        } else {
          $.showNotice(message.msg, "danger");
        }
      }
    }).catch(function (response) {
      $scope.loading = false;
      $.showNotice(response.status + " error：" + response.data.msg, "danger");
    });
  }

  $scope.compileDialog = function (users) {
    //分页
    $scope.nowPage = 1;
    getCompileItems(users);

    //切换分页并处理分页位置
    var cal = 8;
    $scope.calPage1 = 0;
    $scope.calPage2 = cal - $scope.calPage1;
    $scope.changePage = function (page) {
      if (page > 0 && page < $scope.pages.length + 1) {
        $scope.nowPage = page;
        if (page <= cal / 2 + 1) {
          $scope.calPage1 = page - 1;
          $scope.calPage2 = cal - page + 1;
        } else if (page >= $scope.pages.length - cal / 2) {
          $scope.calPage2 = $scope.pages.length - page;
          $scope.calPage1 = cal + page - $scope.pages.length;
        } else {
          $scope.calPage1 = cal / 2;
          $scope.calPage2 = cal / 2;
        }
      }
    };

  };

  $scope.content = "加载中..."

  $scope.fullContent = function (text) {
    $scope.content = text;
  }

});